package com.opensky.qatest.ui.page;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.util.common.AssertReport;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.HTML;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "Sale page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class SalesPage extends LoadableComponent<SalesPage> implements IPage {

    // ************************ Locators ************************

    private static final String SALES_PAGE_URL = "https://opensky.com/sales";

    private static final String LOGO = "//div[@id='logo']//img";

    private static final String LOGOUT_URL = "https://opensky.com/logout/";

    private static final String CURATOR_MNG_LINK = "//div[@id='sky-bar']//a[@href='https://opensky.com/following']";

    private static final String CURATORS_LIST = "//div[@id='sky-bar']//ul[@class='follow-thumbs']";

    private static final String CURATOR_LINK = "//div[@id='sky-bar']//ul[@class='follow-thumbs']/li[1]";

    private static final String CART_NAV = "cart-nav";

    private static final String HELP_LINK = "//div[@id='sky-bar']//a[text()='Help']";

    private static final String YOUR_SALES_LINK = "//header//a[text()='Your Sales']";

    private static final String CATEGORY_PATTERN = "//ul[@id='category-nav']//a[contains(., '%s')]";

    private static final String FOOD_CATEGORY = "//ul[@id='category-nav']//a[contains(., 'Food')]";

    private static final String STYLE_CATEGORY = "//ul[@id='category-nav']//a[contains(., 'Style')]";

    private static final String HEALTH_CATEGORY = "//ul[@id='category-nav']//a[contains(., 'Health')]";

    private static final String DESIGN_CATEGORY = "//ul[@id='category-nav']//a[contains(., 'Design')]";

    private static final String INVITE_FRIENDS_LINK = "//div[@id='invite-friends-nav']/a";

    private static final String CREDITS_LINK = "//div[@id='credits-nav']/a";

    private static final String ACCOUNT_DROPDOWN = "//div[@id='account-nav']/a[@class='dropdown-label']";

    private static final String WELCOME_MESSAGE = "//div[@class='simple-title-header' and contains(., \"Welcome, %s! Here are today's sales.\")]";

    private static final String CURATOR_SALES_CATEGORY = "//div[@id='curator-sales']";

    private static final String OTHER_CURATOR_SALES_CATEGORY = "//div[@id='other-curator-sales']";

    private static final String UPCOMING_SALES = "//div[@id='upcoming-sales']";

    private static final String OFFER_IMG = "//ul[@class='sellable-list duplex']/li[1]//img";

    private static final String OFFER_CURATOR = "//ul[@class='sellable-list duplex']/li[1]//div[@class='curator-attribution']/a";

    private static final String OFFER_NAME = "//ul[@class='sellable-list duplex']/li[1]//h3[@class='sellable-name']/a";

    // private static final String OFFER_TIME =
    // "//ul[@class='sellable-list duplex']/li[1]//span[@class='time']/a";

    private static final String OFFER_BTN_LEARN_MORE = "//ul[@class='sellable-list']/li[1]//a[text()='Learn more']";

    private static final String OFFER_BTN_FOLLOW = "//div[@id='other-curator-sales']//li[1]//a[text()='Follow']";

    private static final String OFFER_BTN_FOLLOWING = "//div[@id='other-curator-sales']//li[1]//a[text()='Following']";

    private static final String UPCOMING_OFFER_BTN_FOLLOW = "//div[@id='upcoming-sales']//li[1]//a[text()='Follow']";

    private static final String EARN_CREDITS_BLOCK = "//div[@id='upcoming-sales']//li[1]//a[text()='Follow']";

    private static final String BEST_SELLERS_BLOCK = "//div[@id='upcoming-sales']//li[1]//a[text()='Follow']";

    private static final String FOOTER_NAV = "//footer//ul[@class='links']";

    private static final String CONTACT_PHONE = "//footer//div[@class='contact-us']//div[contains(., '1 (877) 734-6736')]";

    private static final String CONTACT_EMAIL = "//footer//div[@class='contact-us']//a[contains(., 'help@opensky.com')]";

    private static final String COPYRIGHTS = "//footer//div[@class='copyright' and contains(., '2011 The OpenSky Project, Inc.')]";

    private static final String NYC_LINK = "//footer//div[@id='made-in-nyc']/a";

    private static final String FOLLOW_POPUP_MESSAGE = "//div[@id='ui-tooltip-0-content' and contains(., 'Thanks for following')]";

    private static final String FOLLOWING_POPUP_MESSAGE = "//div[@id='ui-tooltip-1-content' and contains(., 'You are no longer following')]";

    private static final String FOLLOW_POPUP_LINK = "//div[@class='ui-tooltip-content']//a[text()=\"There's an offer happening right now!\"]";

    private static final String UPCOMING_OFFER_IMG = "//div[@id='upcoming-sales']//li[1]//img";

    private static final String DD_CATEGORY_ITEM_SELLER = "//li[@class='category cat-food dropdown']//div[@class='seller']";

    private static final String DD_CATEGORY_ITEM_TITLE = "//li[@class='category cat-food dropdown']//div[@class='title']";

    // private static final String DD_CATEGORY_ITEM_TIME =
    // "//li[@class='category cat-food dropdown']//span[@class='time-remaining']";

    private static final String SEE_ALL_LINK = "//li[@class='category cat-food dropdown']//p[@class='see-all']/a[contains(., 'See all')]";

    private final WebDriverWrapper driver;

    public SalesPage(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(SALES_PAGE_URL);
    }

    public LoginPage logOut() {
	driver.get(LOGOUT_URL);
	return new LoginPage(driver);
    }

    public SalesPage clickYourSalesLink() {
	driver.waitAndClick("YOUR_SALES_LINK", By.xpath(YOUR_SALES_LINK));

	SalesPage salesPage = new SalesPage(driver);

	if (salesPage.isPageLoaded()) {
	    return salesPage;
	} else {
	    return null;
	}
    }

    public boolean clickFollowBtnAndCheckPopupContent() {
	driver.waitAndClick("OFFER_BTN_FOLLOW", By.xpath(OFFER_BTN_FOLLOW));
	driver.waitAndCheckElementPresent("FOLLOW_POPUP_MESSAGE", By.xpath(FOLLOW_POPUP_MESSAGE));
	driver.waitAndCheckElementPresent("FOLLOW_POPUP_LINK", By.xpath(FOLLOW_POPUP_LINK));
	return true;
    }

    public boolean clickUnfollowBtnAndCheckPopupContent() {
	driver.waitAndClick("OFFER_BTN_FOLLOWING", By.xpath(OFFER_BTN_FOLLOWING));
	driver.waitAndCheckElementPresent("FOLLOWING_POPUP_MESSAGE", By.xpath(FOLLOWING_POPUP_MESSAGE));
	driver.waitAndCheckElementPresent("OFFER_BTN_FOLLOW", By.xpath(OFFER_BTN_FOLLOW));
	return true;
    }

    public OfferPage openSuggestedOffer() {
	driver.waitAndClick("OFFER_BTN_FOLLOW", By.xpath(OFFER_BTN_FOLLOW));

	WebElement productLink = driver.waitAndGetElement("FOLLOW_POPUP_LINK", By.xpath(FOLLOW_POPUP_LINK));
	String href = productLink.getAttribute(HTML.HREF);
	String curator = href.split("/")[3];
	String product = href.split("/")[5];

	productLink.click();
	driver.pause(2);

	OfferPage offerPage = new OfferPage(driver, curator, product);

	if (offerPage.isPageLoaded()) {
	    return offerPage;
	} else {
	    return null;
	}
    }

    public SalesPage clickLogo() {
	SalesPage salesPage = new SalesPage(driver);

	driver.waitAndClick("LOGO", By.xpath(LOGO));

	if (salesPage.isPageLoaded()) {
	    return salesPage;
	} else {
	    return null;
	}
    }

    public void clickUpcomingOffer() {
	driver.waitAndClick("UPCOMING_OFFER_IMG", By.xpath(UPCOMING_OFFER_IMG));
    }

    public boolean clickCuratorLinkAndCheckPage() {

	driver.waitAndClick("CURATOR_LINK", By.xpath(CURATOR_LINK));

	boolean result;
	if (driver.waitAndVerifyURL("product")) {
	    result = true;
	} else {
	    result = false;
	}
	driver.get(SALES_PAGE_URL);

	return result;
    }

    public void hoverCategory(String category) {
	driver.waitAndHover("CATEGORY_DROPDOWN", By.xpath(String.format(CATEGORY_PATTERN, category)));
    }

    public CategoryPage clickSeaAllSales(String category) {
	hoverCategory(category);

	CategoryPage categoryPage = new CategoryPage(driver, category);

	driver.waitAndClick(SEE_ALL_LINK, By.xpath(SEE_ALL_LINK));

	if (categoryPage.isPageLoaded()) {
	    return categoryPage;
	} else {
	    return null;
	}
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() throws Error {
	String url = driver.getCurrentUrl();
	Assert.assertTrue(AssertReport.wrongURL(SALES_PAGE_URL, url), LogicUtils.isURLEqual(SALES_PAGE_URL, url));
    }

    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(SALES_PAGE_URL);
    }

    public boolean isPageLoadedNow() {
	String url = driver.getCurrentUrl();
	return url.contains(SALES_PAGE_URL);
    }

    public boolean isSkyBarBaseElementsPresent() {
	driver.waitAndCheckElementPresent("CURATOR_MNG_LINK", By.xpath(CURATOR_MNG_LINK));
	driver.waitAndCheckElementPresent("CURATORS_LIST", By.xpath(CURATORS_LIST));
	driver.waitAndCheckElementPresent("CART_NAV", By.id(CART_NAV));
	driver.waitAndCheckElementPresent("HELP_LINK", By.xpath(HELP_LINK));
	return true;
    }

    public boolean isTopNavBaseElemntsPresent() {
	driver.waitAndCheckElementPresent("YOUR_SALES_LINK", By.xpath(YOUR_SALES_LINK));
	driver.waitAndCheckElementPresent("FOOD_CATEGORY", By.xpath(FOOD_CATEGORY));
	driver.waitAndCheckElementPresent("STYLE_CATEGORY", By.xpath(STYLE_CATEGORY));
	driver.waitAndCheckElementPresent("HEALTH_CATEGORY", By.xpath(HEALTH_CATEGORY));
	driver.waitAndCheckElementPresent("DESIGN_CATEGORY", By.xpath(DESIGN_CATEGORY));
	driver.waitAndCheckElementPresent("INVITE_FRIENDS_LINK", By.xpath(INVITE_FRIENDS_LINK));
	driver.waitAndCheckElementPresent("CREDITS_LINK", By.xpath(CREDITS_LINK));
	driver.waitAndCheckElementPresent("ACCOUNT_DROPDOWN", By.xpath(ACCOUNT_DROPDOWN));
	return true;
    }

    public boolean isWelcomeMessagePresent(String userEmail) {
	String userName = userEmail.split("@")[0];
	driver.waitAndCheckElementPresent("WELCOME_MESSAGE", By.xpath(String.format(WELCOME_MESSAGE, userName)));
	return true;
    }

    public boolean isAllSalesCategoriesPresent() {
	driver.waitAndCheckElementPresent("CURATOR_SALES_CATEGORY", By.xpath(CURATOR_SALES_CATEGORY));
	driver.waitAndCheckElementPresent("OTHER_CURATOR_SALES_CATEGORY", By.xpath(OTHER_CURATOR_SALES_CATEGORY));
	driver.waitAndCheckElementPresent("UPCOMING_SALES", By.xpath(UPCOMING_SALES));
	return true;
    }

    public boolean isOfferBaseElementsPresent() {
	driver.waitAndCheckElementPresent("OFFER_IMG", By.xpath(OFFER_IMG));
	driver.waitAndCheckElementPresent("OFFER_CURATOR", By.xpath(OFFER_CURATOR));
	driver.waitAndCheckElementPresent("OFFER_NAME", By.xpath(OFFER_NAME));
	// driver.waitAndCheckElementPresent("OFFER_TIME",
	// By.xpath(OFFER_TIME));
	return true;
    }

    public boolean isOfferButtonsPreasent() {
	driver.waitAndCheckElementPresent("OFFER_BTN_LEARN_MORE", By.xpath(OFFER_BTN_LEARN_MORE));
	driver.waitAndCheckElementPresent("OFFER_BTN_FOLLOW", By.xpath(OFFER_BTN_FOLLOW));
	driver.waitAndCheckElementPresent("UPCOMING_OFFER_BTN_FOLLOW", By.xpath(UPCOMING_OFFER_BTN_FOLLOW));
	return true;
    }

    public boolean isRightSideBlocksPresent() {
	driver.waitAndCheckElementPresent("EARN_CREDITS_BLOCK", By.xpath(EARN_CREDITS_BLOCK));
	driver.waitAndCheckElementPresent("BEST_SELLERS_BLOCK", By.xpath(BEST_SELLERS_BLOCK));
	return true;
    }

    public boolean isFooterBaseElementsPresent() {
	driver.waitAndCheckElementPresent("FOOTER_NAV", By.xpath(FOOTER_NAV));
	driver.waitAndCheckElementPresent("CONTACT_PHONE", By.xpath(CONTACT_PHONE));
	driver.waitAndCheckElementPresent("CONTACT_EMAIL", By.xpath(CONTACT_EMAIL));
	driver.waitAndCheckElementPresent("COPYRIGHTS", By.xpath(COPYRIGHTS));
	driver.waitAndCheckElementPresent("NYC_LINK", By.xpath(NYC_LINK));
	return true;
    }

    public boolean isDropDownBaseElementsPresent() {
	driver.waitAndCheckElementPresent("DD_CATEGORY_ITEM_SELLER", By.xpath(DD_CATEGORY_ITEM_SELLER));
	driver.waitAndCheckElementPresent("DD_CATEGORY_ITEM_TITLE", By.xpath(DD_CATEGORY_ITEM_TITLE));
	// driver.waitAndCheckElementPresent("DD_CATEGORY_ITEM_TIME",
	// By.xpath(DD_CATEGORY_ITEM_TIME));
	driver.waitAndCheckElementPresent("SEE_ALL_LINK", By.xpath(SEE_ALL_LINK));
	return true;
    }
}
